<?php
/**
 * Created by PhpStorm.
 * User: qiaoli
 * Date: 15/10/20
 * Time: 下午2:09
 */
defined('BASEPATH') OR exit('No direct script access allowed');
class Expertm extends CI_Model
{
    const EXPERT_LIST_OF_ALL = 'expert_list_of_all';
    const EXPERT_LIST_OF_THREAD = 'expert_list_of_thread_';
    const EXPERT_LIST_BY_PHONETIC = 'expert_list_by_phonetic_';

    const QY_EXPERTM_GET_EXP_INFO = 'qy_expertm_get_exp_info_';
    function __construct()
    {
        $this->load->Model("userm");
    }
    public function expert_list_of_thread($threads,$oneThreadSize=5)
    {
        $verifiedusers = array();
        $mc = $this->cache->memcached;
        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        foreach($threads as $thread)
        {
            $mc_key = self::EXPERT_LIST_OF_THREAD . $thread;
            $result = $mc->get($mc_key);
            if(!$result)
            {
                $result = $this->db
                    ->select('ml_fellow.id,ml_fellow.username,ml_thread.thread_name as invite_thread,ml_fellow.head_ico,ml_fellow.expert_info,ml_fellow.user_level,ml_fellow.practice_hospital,ml_fellow.post_title,ml_fellow.truename')
                    ->from('ml_fellow')
                    ->join("ml_thread","ml_thread.id=ml_fellow.thread","left")
                    ->where(array('ml_fellow.user_level'=>5, 'ml_fellow.thread'=>$thread,"ml_fellow.id !="=>230687))
                    ->order_by('ml_fellow.comment_count','desc')
                    ->limit($oneThreadSize,0)
                    ->get()
                    ->result_array();
                $sql = $this->db->last_query();
                $i = 0;
                $cache_data = array();
                foreach($result as $row)
                {
                    $row['head_ico'] = ret_fullpath_url($row['head_ico']);
                    $row['is_expert'] = 1;

                    if(is_numeric($row['post_title']))
                    {
                        ci_load_once('posttitlem');
                        $row['post_title'] = $this->posttitlem->getPosttitleName($row['post_title']);
                    }
                    $expert_info = $this->get_expert_info($row['id']);
                    $row['expert_info'] = $expert_info['goodat'];

                    if($i < $oneThreadSize && $this->uid != $row['id']) {//如果客户端帖子没有分类，就返回所有科室专家列表，但不能像有分类时一样每科室返回太多，那样数据太大，所以加了$oneThreadSize参数控制，且缓存里依然存5个
                        $verifiedusers[] = $row;
                        $cache_data[] = $row;
                        $i++;
                    }
                }
                if($cache_data && count($cache_data) > 0)
                {
                    $mc->save($mc_key,$cache_data,3600);
                }
            }
            else
            {
                $result = array_slice($result,0,$oneThreadSize);
                $verifiedusers = array_merge($verifiedusers,$result);
            }
        }

        foreach($verifiedusers as &$oneUser)
        {
            if($oneUser['id'] == 29185)
            {
                $oneUser['is_full'] = 1;
            }
            else
            {
                $oneUser['is_full'] = 0;
            }
        }
        return $verifiedusers;
    }
    public function get_expert_info($user_id)
    {
        $mc = $this->cache->memcached;
        $this->cache->memcached->is_supported();
        $key = self::QY_EXPERTM_GET_EXP_INFO.$user_id;
        $userinfo = $mc->get($key);
        if(!$userinfo)
        {
            $result = $this->db->select("goodat,experience,social_service,fee")->where("user_id",$user_id)->limit(1,0)->get("ml_specialist")->result_array();
            $userinfo = $result[0];

            $mc->save($key, $userinfo, 20*60*60);
        }
        return $userinfo;
    }

    /* 根据type获取专家列表 */
    public function get_all_special_by_type($type)
    {
        ci_load_once('mc', 'cache');
        $flag = (int)$type;
        $key = 'redis_post_name_list_' . $type;
        $result = $this->redis->get($key);
        if ($result) $res = unserialize($result);
        if (empty($res)) {
            if (isset($flag) && $flag == 1) {
                $res = $this->db->select('c.thread_name,a.id,a.head_ico,a.user_level,a.expert_info,a.truename,b.fee,b.is_service,a.practice_hospital,a.post_title,b.experience,b.goodat,b.consult_count')
                    ->from('ml_fellow as a ')
                    ->join('ml_specialist as b', 'b.user_id=a.id', 'left')
                    ->join('ml_thread as c', 'c.id=a.thread', 'left')
                    ->where('a.user_level', 5)
                    ->where('b.is_service', 1)
                    ->get()
                    ->result_array();
            } else {
                $res = $this->db->select('c.thread_name,a.id,a.head_ico,a.user_level,a.expert_info,a.truename,b.fee,b.is_service,a.practice_hospital,a.post_title,b.experience,b.goodat,b.consult_count')
                    ->from('ml_fellow as a ')
                    ->join('ml_specialist as b', 'b.user_id=a.id', 'left')
                    ->join('ml_thread as c', 'c.id=a.thread', 'left')
                    ->where('a.user_level', 5)
                    ->get()
                    ->result_array();
            }

            //将post_title为数字的转换为职称  将头像改为http形式
            foreach ($res as &$item) {
                $item['head_ico'] = ret_fullpath_url($item['head_ico']);
                if (is_numeric($item['post_title'])) {
                    ci_load_once('posttitlem');
                    $item['post_title'] = $this->posttitlem->getPosttitleName($item['post_title']);
                }
                $item['expert_info'] = $item['goodat'];

                // 如果未设置“可出诊”, 将出诊次数归零, 以免造成困扰
                if (!$item['is_service']) {
                    $item['consult_count'] = 0;
                }
            }

            $res_redis = serialize($res);
            $this->redis->save($key, $res_redis, 200);
        }

        return $res;
    }

    public function get_one_expert($user_id) {
        $user_info = $this->userm->getUserInfo($user_id);
        $expert_info = $this->get_expert_info($user_id);

        if(is_numeric($user_info['post_title']))
        {
            ci_load_once('posttitlem');
            $user_info['post_title'] = $this->posttitlem->getPosttitleName($user_info['post_title']);
        }
        $array =  explode(' ',$user_info["thread_name"]);
        if(count($array) > 1)
        {
            $user_info["thread_name"] = $array[0];
        }
        $data = array(
            "id" => $user_id,
            "username" => $user_info["username"],
            "truename" => $user_info["truename"],
            "post_title" => $user_info["post_title"],
            "invite_thread" => $user_info["thread_name"],
            "head_ico" => $user_info["head_ico"],
            "practice_hospital" => $user_info["practice_hospital"],
            "expert_info" => $expert_info['goodat'],
            "fee" => $expert_info["fee"],
            "is_full" => 0,
            "is_expert" => 1,
            "user_level" => 5
        );
        return $data;
    }
    public function expert_list_of_all()
    {
        $mc = $this->cache->memcached;
        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        $mc_key = self::EXPERT_LIST_OF_ALL;
        $result = $mc->get($mc_key);
        if (!$result)
        {
            $list_result = $this->db->select("user_id")->where("is_service",1)->get("ml_specialist")->result_array();
            foreach ($list_result as $value)
            {
                $result[] = $value['user_id'];
            }
            $mc->save($mc_key, $result, 20*60*60);
        }
        return $result;
    }

    /* 获取所有的分类 */
    public function get_all_class_list()
    {
        $sql = "select id,parent_id,thread_name,is_show,sort,top_id from ml_thread";
        $res = $this->db->query($sql)->result_array();
        return $res;
    }
    /**
     * 获取帖子所有科室
     */
    public function get_all_thread_list($post_id)
    {
        $res  = $this->db->select('type_id')->from('ml_posttype')->where('post_id',$post_id)->get()->result_array();
        return array_column($res,'type_id');
    }
    /**
     * 获取发帖人所在科室
     */
    public function get_author_thread($post_id)
    {
        $res = $this->db->select('b.thread')
                        ->from('ml_post as a')
                        ->join('ml_fellow as b','a.author_id=b.id')
                        ->where('a.id',$post_id)
                        ->get()
                        ->row_array();
        return $res['thread'];
    }
}